package cn.e4j.modular.crm.controller;


import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.modular.crm.entity.Gweq024Export;
import cn.e4j.modular.crm.service.Gweq024Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 应收账龄
 * @author liuw
 * @date 2019/09/04
 */
@Controller
@RequestMapping("/crm/gweq024")
public class Gweq024Controller {

    @Autowired
    private Gweq024Service gweq024Service;

    /**
     * 跳转页面
     */
    @RequiresPermissions("crm:gweq024:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gweq024.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("crm:gweq024:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gweq024Service.getList(page, inDto);
        return new PageReData<>(page, result);
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value="/exportListData",method= RequestMethod.GET)
    @RequiresPermissions("crm:gweq024:init")
    public void exportListData(ModelMap map, HttpServletRequest request, HttpServletResponse response, @DtoParam DtoWapper dtoWapper){
        List<Dto> list = gweq024Service.getList(null, dtoWapper.getDto());
        JSONArray json_arr = new JSONArray();
        for (Map<String, Object> map1 : list) {
            JSONObject json_obj = new JSONObject();
            for (Map.Entry<String, Object> entry : map1.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                json_obj.put(key, value);
            }
            json_arr.add(json_obj);
        }
        List<Gweq024Export> gweq024Exports = new ArrayList<>();
        gweq024Exports = JSONObject.parseArray(json_arr.toString(), Gweq024Export.class);

        // 设置Excel相关参数
        ExportParams params = new ExportParams(null, "应收账龄", ExcelType.XSSF);
        map.put(NormalExcelConstants.FILE_NAME, "应收账龄");
        map.put(NormalExcelConstants.DATA_LIST, gweq024Exports);
        map.put(NormalExcelConstants.CLASS, Gweq024Export.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

}
